package com.yzh.pinboard.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yzh.pinboard.BoardView.Clip;
import com.yzh.pinboard.BoardView.ClipGroup;
import com.yzh.pinboard.R;
import com.yzh.pinboard.utils.DensityUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/3/2.
 */

public class BoardAdapter extends BaseExpandableListAdapter {
    private Context mContext;

    public List<ClipGroup> getParentList() {
        return mData;
    }

    private List<ClipGroup> mData;
    private LayoutInflater mInflater;

    public BoardAdapter(Context context, List<ClipGroup>
            parentList) {
        mContext = context;
        mData = parentList;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount() {
        return mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mData.get(groupPosition).getClips().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).getClips().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {
        ParentViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_group, parent, false);
            viewHolder = new ParentViewHolder();
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.parent_title);
            viewHolder.ivDeleteGroup = (ImageView) convertView.findViewById(R.id.iv_delete_group);

            viewHolder.tvTitle.setText(mData.get(groupPosition).getGroupName());
            viewHolder.ivDeleteGroup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mData.remove(groupPosition);
                    notifyDataSetChanged();
                    if (mOnChangeListener != null) {
                        mOnChangeListener.OnDeleteGroup(groupPosition, mData.get(groupPosition));
                    }
                }
            });
            convertView.setTag(viewHolder);
            //convertView.setVisibility(View.GONE);
        } else {
            viewHolder = (ParentViewHolder) convertView.getTag();
        }
        convertView.setBackgroundColor(Color.CYAN);
        return convertView;
    }

    private String selectedClipId = null;

    public void setSelectedClipId(String clipId) {
        this.selectedClipId = clipId;
        notifyDataSetChanged();
    }

    private int calcIndex(int groupPosition, final int childPosition) {
        int index = 0;
        for (int g = 0; g < mData.size(); g++) {
            ClipGroup group = mData.get(groupPosition);
            List<Clip> clips = group.getClips();
            if (g < groupPosition) {
                index = index + clips.size();
            } else if (g == groupPosition) {
                if (childPosition < clips.size()) {
                    index = index + childPosition + 1;
                } else {
                    index = -1;
                    break;
                }
            }
        }
        return index;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean
            isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_board, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.ivContent = (ImageView) convertView.findViewById(R.id.iv_board);
            viewHolder.ivDeleteItem = (ImageView) convertView.findViewById(R.id.iv_delete_board);
            viewHolder.tvIndex = (TextView) convertView.findViewById(R.id.tv_Index);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final Clip clip = mData.get(groupPosition).getClips().get
                (childPosition);
        if (clip.getPicBitmap() != null) {
            Glide.with(mContext).load(clip.getPicBitmap())
                    .into(viewHolder.ivContent);
        } else {
            Glide.with(mContext).load(clip.getPicPath())
                    .into(viewHolder.ivContent);
        }

        if (clip.id == this.selectedClipId && this.selectedClipId != null) {
            viewHolder.ivContent.setBackgroundResource(R.drawable.imgbg_hilight);
        } else {
            viewHolder.ivContent.setBackgroundResource(R.drawable.imgbg);
        }

        int paddingPx = DensityUtil.dip2px(viewHolder.ivContent.getContext(), 2);
        viewHolder.ivContent.setPadding(paddingPx, paddingPx, paddingPx, paddingPx);

        viewHolder.tvIndex.setText(String.valueOf(calcIndex(groupPosition, childPosition)));

        viewHolder.ivDeleteItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnChangeListener != null) {
                    ClipGroup group = mData.get(groupPosition);
                    List<Clip> clips = group.getClips();
                    mOnChangeListener.OnDeleteItem(groupPosition, childPosition, clips.get(childPosition));
                }
                mData.get(groupPosition).getClips().remove(childPosition);
                notifyDataSetChanged();
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnChangeListener != null) {
                    ClipGroup group = mData.get(groupPosition);
                    List<Clip> clips = group.getClips();
                    mOnChangeListener.OnSelectedItem(groupPosition, childPosition, clips.get(childPosition));
                }
                setSelectedClipId(clip.id);
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private static class ViewHolder {
        ImageView ivContent;
        ImageView ivDeleteItem;
        TextView tvIndex;
    }

    private static class ParentViewHolder {
        TextView tvTitle;
        ImageView ivDeleteGroup;
    }

    public void addData(Clip clip) {
        try {
            mData.get(mData.size() - 1).getClips().add(clip);
            notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void addData(int parentPosition, Clip clip) {
        try {
            mData.get(parentPosition).getClips().add(clip);
            notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addData(int parentPosition, int childPosition, Clip clip) {
        try {
            mData.get(parentPosition).getClips().add(childPosition, clip);
            notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addData(ClipGroup clipGroup) {
        mData.add(clipGroup);
        notifyDataSetChanged();
    }

    public void addData(int parentPosition, ClipGroup clipGroup) {
        mData.add(parentPosition, clipGroup);
        notifyDataSetChanged();
    }


    public void removeData(int parentPosition, Clip clip) {
        mData.get(parentPosition).getClips().remove(clip);
        notifyDataSetChanged();
    }

    public void removeData(int parentPosition, int childPosition) {
        mData.get(parentPosition).getClips().remove(childPosition);
        notifyDataSetChanged();
    }

    public void updateData(int parentPosition, int childPosition, Clip clip) {
        mData.get(parentPosition).getClips().set(childPosition, clip);
        notifyDataSetChanged();
    }

    private OnChangeListener mOnChangeListener;

    public void setOnDeleteListener(OnChangeListener changeListener) {
        mOnChangeListener = changeListener;
    }

    public interface OnChangeListener {
        void OnDeleteGroup(int parentPosition, ClipGroup clipGroup);

        void OnDeleteItem(int parentPosition, int childPosition, Clip clip);

        void OnSelectedItem(int parentPosition, int childPosition, Clip clip);
    }
}
